<?php

/*
Copyright 2005-2012 Textpattern CMS Project
All Rights Reserved.

This file is part of Textpattern.

Textpattern is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('txp'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

//------------------------------------------------------------------------------

class _PagesModel extends SparkModel
{
	private $columns;

	//---------------------------------------------------------------------------

	public function __construct($params)
	{
		parent::__construct($params);
		$this->columns = array_flip(array('name', 'user_html',));
	}

	/**
	 * Retrieve the page name in use by your default Textpattern Section.
	 * 
	 * @return string Page name
	 */
	public function defaultPage()
	{
		$db = $this->loadDB();

		$page_row = $db->selectRow('txp_section', 'page', "name='default'");
		$page = $page_row['page'];

		$this->validatePage($page);

		return $page;
	}

	//---------------------------------------------------------------------------
	/**
	 * Return validated Page column information.
	 * 
	 * @param array HTML form field data
	 * @return array Validated HTML form field data
	 */
	public function newPage($page)
	{
		$this->filterPage($page);
		$this->validatePage($page);

		return $page;
	}

	//---------------------------------------------------------------------------
	/**
	 * Add a Page to the CMS.
	 * 
	 * @param array page Column data for insertion
	 */
	public function addPage(&$page)
	{
		$db = $this->loadDB();
		
		$this->filterPage($page);
		$this->validatePage($page);

		$db->insertRow('txp_page', $page);
	}

	//---------------------------------------------------------------------------
	/**
	 * Update a Page in the CMS.
	 * 
	 * @param array page Column data for updating
	 */
	public function updatePage($page)
	{
		$db = $this->loadDB();

		$this->filterPage($page);
		$this->validatePage($page);

		$name = $page['name'];
		unset($page['name']);

		$db->updateRows('txp_page', $page, 'name=?', $name);
	}

	//---------------------------------------------------------------------------
	/**
	 * Delete a Page from the CMS.
	 * 
	 * @param array page Column data containing page for removal
	 */
	public function deletePage($page)
	{
		$db = $this->loadDB();

		$this->validatePage($page);

		$db->deleteRows('txp_page', 'name=?', $page['name']);
	}

	//---------------------------------------------------------------------------
	/**
	 * Retrieve data about the given Page name.
	 * 
	 * @param string name The Page to locate
	 * @param string columns The columns to return from the query
	 * $return row|false The located row of information, or false if not found
	 */
	public function findPageByName($name, $columns = '*')
	{
		$db = $this->loadDB();

		if (!$row = $db->selectRow('txp_page', $columns, 'name=?', $name))
		{
			return false;
		}
		
		return $row;
	}

	//---------------------------------------------------------------------------
	/**
	 * Retrieve all defined Pages, subject to the given filters.
	 * 
	 * @param mixed filters Things to filter the list
	 * @param string columns The columns to return from the query
	 * $return resultset The matched rows of information
	 */
	public function findPages($filters = NULL, $columns = '*')
	{
		$db = $this->loadDB();

		$where = $db->buildConjunction('txp_page', $filters, $bind);

		return $db->query($db->buildSelect('txp_page', $columns, NULL, $where, 'name'), $bind)->rows();
	}

	//---------------------------------------------------------------------------
	/**
	 * Verify the required columns exist in the database
	 * 
	 * @param array HTML form field data
	 */
	private function filterPage(&$page)
	{
		$page = array_intersect_key($page, $this->columns);
	}

	//---------------------------------------------------------------------------
	/**
	 * Validate the Page name
	 * 
	 * @param array HTML form field data
	 */
	private function validatePage(&$page)
	{
		if (empty($page['name']))
		{
			throw new SparkHTTPException_BadRequest();
		}
	}

//------------------------------------------------------------------------------
	
}
